package com.upcoming.common;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-05-10 11:27
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class ServiceResponse<T extends Serializable> implements Serializable  {

    private static final long serialVersionUID = 1956543605100760522L;

    private String errCode = SUCCESS;
    private String errMsg = "SUCCESS";
    private Throwable exc = null;
    private T data;

    public final static String FAILURE = "103";// 失败提示
    public final static String ERROR = "500";//程序处理出错
    public final static String SUCCESS = "000";//成功


    /**
     * 通讯或业务处理是否成功标识。
     *
     * @return
     */
    public boolean isSuccess() {
        if ("000".equals(errCode))
            return true;
        return false;
    }

    /**
     * 转换成Json字符串
     *
     * @return
     */
    public String toJson() {
        try {
            Map<String, Object> resObject = new HashMap<String, Object>(4);
            resObject.put("currTime", System.currentTimeMillis());
            resObject.put("data", data == null ? new Object() : data);
            resObject.put("resMsg", errMsg);
            resObject.put("resCode", errCode);

            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Long.class, new ValueProcessor());
            jsonConfig.registerJsonValueProcessor(Integer.class, new ValueProcessor());
            jsonConfig.registerJsonValueProcessor(String.class, new ValueProcessor());

            return JSONObject.fromObject(resObject, jsonConfig).toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务端JSON转换出错", e);
        }
    }

    /**
     * 返回业务处理成功
     *
     * @return
     */
    public ServiceResponse<T> success() {
        this.errCode = SUCCESS;
        return this;
    }

    /**
     * 返回一个业务处理失败
     *
     * @param message
     * @return
     */
    public static ServiceResponse failure(String message) {
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.errCode = FAILURE;
        serviceResponse.errMsg = message;
        return serviceResponse;
    }

    /**
     * 返回一个业务处理出错
     *
     * @param exc
     * @return
     */
    public static ServiceResponse error(Throwable exc) {
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.errCode = ERROR;
        serviceResponse.errMsg = exc.getMessage();
        serviceResponse.exc = exc;
        return serviceResponse;
    }


    private class ValueProcessor implements JsonValueProcessor {

        public Object processArrayValue(Object o, JsonConfig jsonConfig) {
            return o;
        }

        public String processObjectValue(String key, Object value, JsonConfig jsonConfig) {
            if (value == null) {
                return "";
            }
            return String.valueOf(value);
        }
    }

    public String getErrCode() {
        return errCode;
    }

    public ServiceResponse setErrCode(String errCode) {
        this.errCode = errCode;
        return this;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public ServiceResponse setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }

    public T getData() {
        return data;
    }

    public ServiceResponse setData(T data) {
        this.data = data;
        return this;
    }

    public Throwable getExc() {
        return exc;
    }

    public void setExc(Exception exc) {
        this.exc = exc;
    }
}
